package com.itheima._11Collection遍历;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

/**
 * @Description Demo111
 * @Author GreenHand_Lu
 * @Date 2025-07-19
 */
public class Demo111 {
    public static void main(String[] args) {
        //遍历集合
        //定义集合
        List<String> c = new ArrayList<>();
        c.add("a");
        c.add("b");
        c.add("c");
        c.add("d");

        //迭代器---collection集合的迭代器---数组没有此方法
        //默认指向第一个元素
        //.next()做两个操作：取出元素，并移动指针指向下一位
        System.out.println("迭代器");
        Iterator<String> it = c.iterator();
        while (it.hasNext()) {
            String s = it.next();
            System.out.println(s);
        }

        //for循环
        System.out.println("for循环");
        for (int i = 0; i < c.size(); i++) {
            String s = c.get(i);
            System.out.println(s);
        }

        //增强for循环
        System.out.println("增强for循环");
        for (String s : c) {
            System.out.println(s);
        }

        //匿名内部类
        System.out.println("匿名内部类");
        c.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });

        //Lambda表达式
        System.out.println("Lambda表达式");
        c.forEach(s -> System.out.println(s));

        //方法引用
        System.out.println("方法引用");
        c.forEach(System.out::println);


    }
}
